/*
 * @(#) $Header$
 *
 * Copyright (C) 2007  Forklabs Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.media.jai.opimage;

import java.awt.RenderingHints;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.awt.image.renderable.RenderableImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.media.jai.CollectionImage;
import javax.media.jai.CollectionImageFactory;
import javax.media.jai.CollectionOp;
import javax.media.jai.JAI;
import javax.media.jai.RenderableCollectionImageFactory;
import ca.forklabs.media.jai.ParameterBlockUtil;
import ca.forklabs.media.jai.SimpleCollectionImage;
import ca.forklabs.media.jai.operator.PeriodicShift3DDescriptor;

/**
 * Class {@code PeriodicShift3DRCIF} is a {@link CollectionImageFactory} and a
 * {@link RenderableCollectionImageFactory} supporting the
 * <em>periodicshift3d</em> operation.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.media.jai.opimage.PeriodicShift3DRCIF">Daniel Léonard</a>
 * @version $Revision$
 */
public class PeriodicShift3DRCIF implements CollectionImageFactory, RenderableCollectionImageFactory {

//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    */
   public PeriodicShift3DRCIF() {
   // nothing
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the diplacement in <em>x</em> within the parameter block.
    * @param   pb   the parameter block.
    * @return   the displacement.
    */
   protected Integer getShiftX(ParameterBlock pb) {
      int index = PeriodicShift3DDescriptor.SHIFT_X_PARAMETER_INDEX;
      Integer shift_x = (Integer) pb.getObjectParameter(index);
      return shift_x;
      }

   /**
    * Gets the diplacement in <em>y</em> within the parameter block.
    * @param   pb   the parameter block.
    * @return   the displacement.
    */
   protected Integer getShiftY(ParameterBlock pb) {
      int index = PeriodicShift3DDescriptor.SHIFT_Y_PARAMETER_INDEX;
      Integer shift_y = (Integer) pb.getObjectParameter(index);
      return shift_y;
      }

   /**
    * Gets the diplacement in <em>z</em> within the parameter block.
    * @param   pb   the parameter block.
    * @return   the displacement.
    */
   protected Integer getShiftZ(ParameterBlock pb) {
      int index = PeriodicShift3DDescriptor.SHIFT_Z_PARAMETER_INDEX;
      Integer shift_z = (Integer) pb.getObjectParameter(index);
      return shift_z;
      }

   /**
    * Shifts the cubic image.
    * @param   pb   the parameter block.
    * @param   hints   the rendering hints.
    * @return   the shifted cubic image.
    */
   @SuppressWarnings({ "unchecked", "nls", "boxing", "rawtypes" })
   protected CollectionImage shift(ParameterBlock pb, RenderingHints hints) {
      CollectionImage sources = (CollectionImage) pb.getSource(0);

      Integer shift_x = this.getShiftX(pb);
      Integer shift_y = this.getShiftY(pb);
      Integer shift_z = this.getShiftZ(pb);

      int len = sources.size();
      List sinks = new ArrayList(len);

      for (Object source : sources) {
         Object sink;
         if (source instanceof RenderedImage) {
            RenderedImage image = (RenderedImage) source;
            ParameterBlock parameters = ParameterBlockUtil.createPeriodicShiftParameterBlock(image, shift_x, shift_y);
            sink = JAI.create("periodicshift", parameters, hints);
            }
         else if (source instanceof RenderableImage) {
            RenderableImage image = (RenderableImage) source;
            ParameterBlock parameters = ParameterBlockUtil.createPeriodicShiftParameterBlock(image, shift_x, shift_y);
            sink = JAI.createRenderable("periodicshift", parameters, hints);
            }
         else {
            Class<?> clazz = source.getClass();
            String messsage = this.getNeitherRenderedNorRenderableErrorMessage(clazz);
            throw new IllegalStateException(messsage);
            }
         sinks.add(sink);
         }

      Collections.rotate(sinks, shift_z);

      CollectionImage collection_image = new SimpleCollectionImage(sinks);
      return collection_image;
      }

   /**
    * Gets the error message saying that a source image is neither a rendered
    * nor a renderable image.
    * @param   clazz   the class of the offending image.
    * @return   the error message.
    */
   protected String getNeitherRenderedNorRenderableErrorMessage(Class<?> clazz) {
      String key = Resources.NEITHER_RENDERED_NOR_RENDERABLE;
      String message = Resources.getLocalizedString(key, clazz);
      return message;
      }


//---------------------------
// Implemented methods from javax.media.jai.CollectionImageFactory
//---------------------------

   /**
    * Calls {@link #shift(ParameterBlock, RenderingHints)}.
    * @param   pb   the parameter block.
    * @param   hints   the rendering hints.
    * @return   the shifted cubic image.
    */
   public CollectionImage create(ParameterBlock pb, RenderingHints hints) {
      CollectionImage collection_image = this.shift(pb, hints);
      return collection_image;
      }

   /**
    * It is impratical to perform the update.
    * @param old_pb   ignored
    * @param old_hints   ignored
    * @param new_pb   ignored
    * @param new_hints   ignored
    * @param old_image   ignored
    * @param op   ignored
    * @return always {@code null}.
    */
   public CollectionImage update(ParameterBlock old_pb, RenderingHints old_hints,
                                 ParameterBlock new_pb, RenderingHints new_hints,
                                 CollectionImage old_image,
                                 CollectionOp op) {
   // it is impracticable to perform the update
      CollectionImage new_image = null;
      return new_image;
      }


//---------------------------
// Implemented methods from javax.media.jai.RenderableCollectionImageFactory
//---------------------------

   /**
    * Calls {@link #shift(ParameterBlock, RenderingHints)}.
    * @param   pb   the parameter block.
    * @return   the shifted cubic image.
    */
   @Override
   public CollectionImage create(ParameterBlock pb) {
      RenderingHints hints = null;
      CollectionImage sinks = this.shift(pb, hints);
      return sinks;
      }

   }
